import pygame
from typing import Tuple, Callable

from game.world.coordinate import CoordinateType
from game.ui.base import BaseWidget
from game.container import CONSTANT


class TextWidget(BaseWidget):
    
    def __init__(
        self,
        
        display_surface: pygame.Surface,
        
        center: CoordinateType,
        groups: Tuple[pygame.sprite.LayeredUpdates, ...],
        
        text: str,
        color: pygame.Color,
        background: pygame.Color | None = None,
        font = CONSTANT.APP.BUTTON_FONT,
        on_click_callbacks: Tuple[Callable[[], None], ...] = ()
        
    ):
        super().__init__(display_surface, groups, on_click_callbacks)
        
        self.color = color
        self.background = background
        self.font = font

        self.surface = self.font.render(text, True, color, background)
        self.rect = self.surface.get_rect(center=center)
    
    def set_text(self, text: str) -> None:
        self.surface = self.font.render(text, True, self.color, self.background)
        self.rect = self.surface.get_rect(center=self.rect.center)

    def draw(self) -> None:
        if self.is_visible:
            self.display_surface.blit(self.surface, self.rect)

    def undraw(self) -> None:
        self.is_visible = False

    def set_visible(self):
        self.is_visible = True
